package com.litesuits.sdk.handler;

import android.app.Activity;
import com.litesuits.http.data.HttpStatus;
import com.litesuits.http.exception.*;
import com.litesuits.http.exception.handler.HttpExceptionHandler;
import com.litesuits.sdk.LiteSdk;
import com.litesuits.sdk.R;

/**
 * Use this to handle HTTP exceptions.
 *
 * @author MaTianyu on 2015-05-05
 */
public class DefaultHttpExceptionHandler extends HttpExceptionHandler {
    protected Activity activity;
    protected LiteSdk sdk;

    public DefaultHttpExceptionHandler(LiteSdk sdk) {
        this.sdk = sdk;
    }

    public HttpExceptionHandler via(Activity activity) {
        this.activity = activity;
        return this;
    }

    @Override
    protected void onClientException(HttpClientException e, ClientException e1) {
        if (activity != null && !activity.isFinishing()) {
            //DialogUtil.showTips(activity, R.string.exception_tips, R.string.exception_client);
            sdk.getToastor().showSingletonToast(R.string.exception_client);
        } else {
            sdk.getToastor().showSingletonToast(R.string.exception_client);
        }
        activity = null;
    }

    @Override
    protected void onNetException(HttpNetException e, NetException e1) {
        if (activity != null && !activity.isFinishing()) {
//            DialogUtil.showTips(activity, R.string.exception_tips, R.string.exception_network);
            sdk.getToastor().showSingletonToast(R.string.exception_network);
        } else {
            sdk.getToastor().showSingletonToast(R.string.exception_network);
        }
        activity = null;
    }

    @Override
    protected void onServerException(HttpServerException e, ServerException e1, HttpStatus httpStatus) {
        if (activity != null && !activity.isFinishing()) {
//            DialogUtil.showTips(activity, R.string.exception_tips, R.string.exception_server);
            sdk.getToastor().showSingletonToast(R.string.exception_server);
        } else {
            sdk.getToastor().showSingletonToast(R.string.exception_server);
        }
        activity = null;
    }
}